package com.itheima.listviewsample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.ArrayMap;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.itheima.listviewsample.fragment.ListViewGrideViewFragment;
import com.itheima.listviewsample.fragment.ListViewSelectorFragment;

import java.util.ArrayList;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private Map<String, Fragment> data;
    private ListView listView;
    private ArrayList<String> keys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listview);

        data = new ArrayMap<>();
        data.put("ListView Selector", new ListViewSelectorFragment());
        data.put("ListView+GridView", new ListViewGrideViewFragment());

        keys = new ArrayList<>();
        keys.addAll(data.keySet());
        changePage(keys.get(0));

        final MyAdapter myAdapter = new MyAdapter(this, keys);
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                view.setSelected(true);
                changePage(keys.get(position));
            }
        });


    }

    private void changePage(String key) {
        Fragment fragment = data.get(key);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, fragment).commit();
    }

}
